------------------------------------------------------------------------
--                                                                    --
--  Modern Styled Hud For SMBX                                        --
--  Made by Elf of Happy and Love (���ְ���С����)               --
--  Heavily modified by EeveeEuphoria, for episode-specific usage     --
--                                                                    --
--  This replaces Original SMW Styled Hud with a Modern Styled Hud.   --
--  Like New Super Mario Bros. Wii, Super Mario Maker, etc.           --
--                                                                    --
------------------------------------------------------------------------

local modernhud = {}

local ticks = 0
local timeup = false

local hurryupsfx = Misc.multiResolveFile("ModernHUD/hurry-up.ogg", "sound/extended/hurry-up.ogg")

local modernhudchar = {}
modernhudchar[1] = Graphics.loadImage(Misc.multiResolveFile("ModernHUD/modernhud-1-1.png", "graphics/modernhud/modernhud-1-1.png"))
modernhudchar[2] = Graphics.loadImage(Misc.multiResolveFile("ModernHUD/modernhud-1-2.png", "graphics/modernhud/modernhud-1-2.png"))
modernhudchar[3] = Graphics.loadImage(Misc.multiResolveFile("ModernHUD/modernhud-1-3.png", "graphics/modernhud/modernhud-1-3.png"))
modernhudchar[4] = Graphics.loadImage(Misc.multiResolveFile("ModernHUD/modernhud-1-4.png", "graphics/modernhud/modernhud-1-4.png"))
modernhudchar[5] = Graphics.loadImage(Misc.multiResolveFile("ModernHUD/modernhud-1-5.png", "graphics/modernhud/modernhud-1-5.png"))

local starcounter = Graphics.loadImage(Misc.multiResolveFile("ModernHUD/modernhud-3.png", "graphics/modernhud/modernhud-3.png"))
local starcoin = Graphics.loadImage(Misc.resolveFile("GFX/starcoin.png"))

local WingGFX = Graphics.loadImage(Misc.resolveFile("GFX/wing.png"))

local multiplier = Graphics.sprites.hardcoded["33-1"].img
local reservebox = Graphics.loadImage(Misc.resolveFile("GFX/reserve1p.png"))
local reservebox2 = Graphics.loadImage(Misc.resolveFile("GFX/reserve.png"))

local COINrealframe = 1
local COINtimer = 51
local CoinGFX = Graphics.loadImage(Misc.resolveFile("coinFX/coin" .. COINrealframe .. ".png"))

local heartbg =  Graphics.sprites.hardcoded["36-2"].img
local heartfill = Graphics.sprites.hardcoded["36-1"].img

modernhud.usereserve = modernhud.usereserve or true
modernhud.showhud = modernhud.showhud or true

local WaitTime = 0
local StarXOffset = -135

function modernhud.onInitAPI()
	registerEvent(modernhud,"onStart")
	registerEvent(modernhud,"onTick")
	registerEvent(modernhud,"onHUDDraw")
end

local VisualPoints

function modernhud.onStart()
	Graphics.activateHud(false)
	VisualPoints = SaveData._basegame.hud.score
end

function modernhud.onTick()
	if VisualPoints > SaveData._basegame.hud.score then
		VisualPoints = math.max(SaveData._basegame.hud.score, VisualPoints - 25)
	elseif VisualPoints < SaveData._basegame.hud.score then
		VisualPoints = math.min(SaveData._basegame.hud.score,VisualPoints + 25)
	end
end

function modernhud.onHUDDraw()
	if modernhud.showhud then
		modernhud.drawhud()
	end
end

function modernhud.drawhud()
	--Text.printWP(mem(0x00B2C5AC,FIELD_FLOAT),1,84,48,5)
	--Text.printWP(mem(0x00B2C5A8,FIELD_WORD),1,84,86,5)
	if Level.name() == "Mario Castle" then Text.printWP(mem(0x00B251E0,FIELD_WORD),1,84,124-38,5) end

	if Level.name() == "Mario Castle" then Text.printWP(SaveData._basegame.starcoinCounter - globalsave.StarCoinsSpent,1, 84+4, 119+4, 5) end
	if Level.name() == "Mario Castle" then Graphics.drawImageWP(starcoin, 20, 102+4, 5) end
	if Level.name() == "Mario Castle" then Graphics.drawImageWP(multiplier, 66+4, 119+4, 5) end

	-- [[ Coin Counter, w/ spinning coin gfx ]] --
	if not Misc.isPaused() then
		if VisualCoins ~= myCurrency._value or (not VisualCoins ~= myCurrency._value and COINrealframe ~= 1) then
			COINtimer = COINtimer - 1
			if COINtimer < 0 then
				COINtimer = 51
			end
			if (COINtimer % 5 == 0) then
				COINrealframe = COINrealframe + 1
				if COINrealframe > 3 then
					COINrealframe = 0
				end
			end
			CoinGFX = Graphics.loadImage(Misc.resolveFile("coinFX/coin" .. COINrealframe .. ".png"))
		else
			COINtimer = 51
		end
	end
	Graphics.drawImageWP(CoinGFX, 32, 68-38, 5)
	Graphics.drawImageWP(multiplier, 66, 48, 5)
	Text.printWP(VisualCoins, 1, 84, 86-38, 5) -- Visual Coin Counter

	
	for _,plyr in ipairs(Player.get()) do
		local camerapos = 600
		local reserveimage = reservebox
		local camerathing = camera

		-- [[ Star Counter ]] --
		if Level.name() ~= "Mario Castle" then
			local anykey = false
			for _,v in pairs(plyr.keys) do -- thanks to Proloe for this!
				if v then
					anykey = true
					break
				end
			end
			if not anykey and not PlayerIsDead then
				WaitTime = WaitTime + 1
			else
				WaitTime = 0
			end
			if Misc.isPaused() then
				WaitTime = 99999
			end
			local SECONDZ = 1.75 -- amount of time to wait until the star counter shows up
			if ((WaitTime > SECONDZ * 61 or StarXOffset > -135)) or (Level.winState() > 0) then
				if StarXOffset < 0 and ((WaitTime > SECONDZ * 61) or (Level.winState() > 0))then
					StarXOffset = StarXOffset + 4
				end
				Text.printWP(mem(0x00B251E0,FIELD_WORD),1,84+StarXOffset,124-38,5)
				if not (Level.winState() > 0) then
					if WaitTime < SECONDZ * 61 then
						if StarXOffset > -135 then
							StarXOffset = StarXOffset - 4
						end
						if StarXOffset < -135 then
							StarXOffset = -135
						end
					end
				end
			end
		else
			StarXOffset = 0
		end

		Graphics.drawImageWP(starcounter, 30+StarXOffset, 106-38, 5)
		Graphics.drawImageWP(multiplier, 66+StarXOffset, 124-38, 5)

		-- [[ Reserve items ]] --
		if modernhud.usereserve and plyr.character < 3 then
			if player2 and camera.height < 600 then
				camerapos = camera.height
			elseif plyr == player2 then
				camerapos = camerapos - 70
			end
			if plyr == player2 then
				reserveimage = reservebox2
				if (camerapos ~= 600 - 70) or (camerathing.width ~= 800) then
					camerathing = camera2
					if camerathing.width ~= 800 then
						camerapos = camerapos + 70 -- this is sloppy i am sorry
					end
				end
			end
			if not gplayerData[plyr.idx].PowerX then gplayerData[plyr.idx].PowerX = -90 end
			Graphics.drawImageToSceneWP(reserveimage, gplayerData[plyr.idx].PowerX + camerathing.x, (camerapos - 74) + camerathing.y, 0.7, 3)
			if gplayerData[plyr.idx].reservePowerup > 0 then
				if gplayerData[plyr.idx].PowerX < 6 then
					gplayerData[plyr.idx].PowerX = gplayerData[plyr.idx].PowerX + 3
				elseif gplayerData[plyr.idx].PowerX > 6 then
					gplayerData[plyr.idx].PowerX = 6
				end
				local reservepower = Graphics.sprites.npc[gplayerData[plyr.idx].reservePowerup].img
				Graphics.draw{type = RTYPE_IMAGE, image = reservepower, sceneCoords = true, x = (gplayerData[plyr.idx].PowerX + 18) + camerathing.x, y = (camerapos - 56) + camerathing.y, opacity=0.7, sourceWidth = NPC.config[gplayerData[plyr.idx].reservePowerup].width, sourceHeight = NPC.config[gplayerData[plyr.idx].reservePowerup].height}
			else
				if gplayerData[plyr.idx].PowerX > -90 then
					gplayerData[plyr.idx].PowerX = gplayerData[plyr.idx].PowerX - 3
				elseif gplayerData[plyr.idx].PowerX < -90 then
					gplayerData[plyr.idx].PowerX = -90
				end
			end
			if gplayerData[plyr.idx].reservePowerupTwo > 0 then
				local reservepower = Graphics.sprites.npc[gplayerData[plyr.idx].reservePowerupTwo].img							  -- nice
				Graphics.draw{type = RTYPE_IMAGE, image = reservepower, sceneCoords = true, x = (gplayerData[plyr.idx].PowerX + 69) + camerathing.x, y = (camerapos - 56) + camerathing.y, opacity=0.5, sourceWidth = NPC.config[gplayerData[plyr.idx].reservePowerupTwo].width, sourceHeight = NPC.config[gplayerData[plyr.idx].reservePowerupTwo].height}
			end
		end

		-- [[ Score ]] --
		if VisualPoints < 99999990 then
			Text.printWP(string.format("%08d", VisualPoints), 1, camerathing.width-174, 48, 5)
		else
			Text.printWP("99999990", 1, camerathing.width-174, 48, 5)
		end

		-- [[ Life Saver icon / wings icon ]] --
		if globalsave.LifeSaver then
			Graphics.drawImageWP(WingGFX, (camerathing.width/2) - math.floor(WingGFX.width/2), 10, 5)
			-- Graphics.drawImageWP(WingGFX, 393, 580, 5)
		end

		-- [[ Hearts ]] --
		if plyr.character >= 3 then
			Graphics.drawImageWP(heartbg,357,38,4.9)
			Graphics.drawImageWP(heartbg,389,38,4.9)
			Graphics.drawImageWP(heartbg,421,38,4.9)
			if (plyr:mem(0x16,FIELD_WORD)) == 1 then
				Graphics.drawImageWP(heartfill,357,38,5)
			elseif (plyr:mem(0x16,FIELD_WORD)) == 2 then
				Graphics.drawImageWP(heartfill,357,38,5)
				Graphics.drawImageWP(heartfill,389,38,5)
			elseif (plyr:mem(0x16,FIELD_WORD)) == 3 then
				Graphics.drawImageWP(heartfill,357,38,5)
				Graphics.drawImageWP(heartfill,389,38,5)
				Graphics.drawImageWP(heartfill,421,38,5)
			end
		end
	end
end

return modernhud